random
查看可用方法:
import random
help(random)
猜數字遊戲:
1.randomint()隨機顯示範圍內數字(每次顯示會不一樣)
import random
print(random.randint(1,10))
4
2.random()隨幾顯示0~1之間的浮點數(每次顯示會不一樣)
import random
print(random.random())
0.16773833465309274
3.列表中隨機顯示一個元素ex:剪刀石頭布(每次顯示會不一樣)
import random
option = ["剪刀","石頭","布"]
random_option = random.choice(option)
print(random_option)
石頭
4.random.shuffle()將列表打亂ex:簡易樸克牌(每次顯示會不一樣)
import random
cards = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
random.shuffle(cards)
print(cards)
['7', 'A', 'K', '6', '5', '10', '2', '9', '3', '4', '8', 'J', 'Q']